﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CrmApplication.Models;

namespace CrmApplication.Controllers
{
    public class LoginController : Controller
    {        
        public ActionResult Login()
        {            
            return View();
        }

        /// <summary>
        /// connect user to the application
        /// </summary>
        /// <returns></returns>
        public ActionResult DoLogin()
        {
            UserModels user = new UserModels();
            user.Username = Request["Username"];
            user.Password = Request["Password"];

            UserModels user1 = new UserModels();
            user1 = UserModels.GetUser(user);
            try
            {
                if ((user1 != null))
                {                    
                    if ((user.Username == user1.Username) & (user.Password == user1.Password))
                    {
                        Session.Add("user", user1);                        
                    }
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    return RedirectToAction("Login", "Login");
                }

            }
            catch (Exception ex)
            {
                return null;
            }          
            
        }

        /// <summary>
        /// disconnect user from application
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public ActionResult DoLogout(UserModels user)
        {
            UserModels userSession = Session["user"] as UserModels;
            Session.Remove("userSession");
            return RedirectToAction("Login", "Login");
        }
    }
}
